package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import persistence.exception.DAOException;
import persistence.util.JdbcUtil;

public class PredefinedEntryDAOJdbcImpl extends JdbcDAOBase implements PredefinedEntryDAO {

	private PredefinedParamDAO predefinedParamDAO;	
	
	public PredefinedParamDAO getPredefinedParamDAO() {
		return predefinedParamDAO;
	}
	public void setPredefinedParamDAO(PredefinedParamDAO predefinedParamDAO) {
		this.predefinedParamDAO = predefinedParamDAO;
	}
	public PredefinedEntryDAOJdbcImpl() {}
	
	@Override
	public PredefinedEntry find(int id) throws DAOException {
		String sql = "SELECT id, parameterwert, auswahlparameter_id FROM auswahlwerte WHERE id = ?";
		Connection con = null;
		PreparedStatement prepStmt = null;
		ResultSet rs = null;
		PredefinedEntry predefEntry = null;
		PredefinedParam predefParam = null;
		
		try {
			con = getConProvider().getConnection();
			prepStmt = con.prepareStatement(sql);
			prepStmt.setInt(1, id);
			rs = prepStmt.executeQuery();
			
			if (rs.next()) {
				predefParam = predefinedParamDAO.findParamById(rs.getInt(3));
				predefEntry = new PredefinedEntry(predefParam, rs.getInt(1), rs.getString(2));
			}
		}
		catch (SQLException se) {
			throw new DAOException("Error when attempting to find PredefinedEntry with id: " + id, se);
		}
		finally {
			JdbcUtil.cleanup(con, prepStmt, rs);
		}
		return predefEntry;
	}

	@Override
	public List<PredefinedEntry> findAllWithParameter(PredefinedParam param) throws DAOException {
		String sql = "SELECT id, parameterwert, auswahlparameter_id FROM auswahlwerte WHERE auswahlparameter_id = ?";
		Connection con = null;
		PreparedStatement prepStmt = null;
		ResultSet rs = null;
		List<PredefinedEntry> entries = new ArrayList<PredefinedEntry>();
		
		try {
			con = getConProvider().getConnection();
			prepStmt = con.prepareStatement(sql);
			prepStmt.setInt(1, param.getId());
			rs = prepStmt.executeQuery();
			
			while (rs.next()) {
				entries.add(new PredefinedEntry(param, rs.getInt(1), rs.getString(2)));
			}
		}
		catch (SQLException se) {
			se.printStackTrace();
			throw new DAOException("Error when attempting to find all PredefinedEntries for Parameter with id: " + param.getId(), se);
		}
		finally {
			JdbcUtil.cleanup(con, prepStmt, rs);
		}
		return entries;
	}

	@Override
	public List<PredefinedEntry> findAll() throws DAOException {
		String sql = "SELECT id, parameterwert, auswahlparameter_id FROM auswahlwerte";
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		List<PredefinedEntry> predefinedEntries = new ArrayList<PredefinedEntry>();
		PredefinedParam currentParam = null;
		
		try {
			con = getConProvider().getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				currentParam = predefinedParamDAO.findParamById(rs.getInt(3));				
				predefinedEntries.add(new PredefinedEntry(currentParam, rs.getInt(1), rs.getString(2)));
			}
		}
		catch (SQLException se) {
			throw new DAOException("Problem when attempting to get all PredefinedEntries", se);
		}
		finally {
			
			JdbcUtil.cleanup(con, stmt, rs);
		}
		return predefinedEntries;
	}

	@Override
	public void updatePredefinedEntry(int id) throws DAOException {
		// TODO Auto-generated method stub

	}

	@Override
	public PredefinedEntry create(int id, int value, PredefinedParam param)
			throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

}
